﻿Imports System.Net.Sockets
''' <summary>
''' 一个内存块
''' </summary>
Public Class BufferChunk
    Private _Manage As BufferChunkManage
    Public ReadOnly Property Manage As BufferChunkManage
        Get
            Return _Manage
        End Get
    End Property

    Private _Buffer As Byte()

    ''' <summary>
    ''' 内存块所在的区域
    ''' </summary>
    ''' <returns></returns>
    Public ReadOnly Property Buffer As Byte()
        Get
            Return _Buffer
        End Get
    End Property

    Public mOffset As Integer

    ''' <summary>
    ''' 内存块偏移量
    ''' </summary>
    ''' <returns></returns>
    Public ReadOnly Property Offset As Integer
        Get
            Return mOffset
        End Get
    End Property

    Public mSize As Integer

    ''' <summary>
    ''' 内存块容量
    ''' </summary>
    ''' <returns></returns>
    Public ReadOnly Property Size As Integer
        Get
            Return mSize
        End Get
    End Property

    Public Property ReadableBytes As Integer

    ''' <summary>
    ''' 构建一个内存块
    ''' </summary>
    ''' <param name="bufferChunkManage">所属管理器</param>
    ''' <param name="buffer"></param>
    ''' <param name="iOffset"></param>
    ''' <param name="chunkSize"></param>
    Public Sub New(bufferChunkManage As BufferChunkManage, buffer() As Byte, iOffset As Integer, chunkSize As Integer)
        Me._Manage = bufferChunkManage
        Me._Buffer = buffer
        Me.mOffset = iOffset
        Me.mSize = chunkSize
        ReadableBytes = 0
    End Sub

    Friend Sub Clear()
        Me._Manage = Nothing
        Me._Buffer = Nothing
        ReadableBytes = 0
    End Sub

    ''' <summary>
    ''' 从别的 sur 拷贝数据到本块
    ''' </summary>
    Public Sub CopyFrom(ByVal sur As BufferChunk， ByVal iSize As Integer)
        If iSize > mSize Then
            Throw New ArgumentOutOfRangeException("iSize"， "需要拷贝的数据块过大")
        End If
        Array.Copy(sur.Buffer, sur.Offset, _Buffer, mOffset, iSize)
        ReadableBytes = iSize
    End Sub


    ''' <summary>
    ''' 将一个数组拷贝到缓冲区
    ''' </summary>
    Public Sub WriteByte(ByVal bytes As Byte)

        _Buffer(mOffset + ReadableBytes) = bytes
        ReadableBytes += 1
    End Sub


    ''' <summary>
    ''' 将一个数组拷贝到缓冲区
    ''' </summary>
    Public Sub WriteBytes(ByVal bytes() As Byte)
        If bytes Is Nothing Then Return

        WriteBytes(0, bytes, 0, bytes.Length)
    End Sub

    ''' <summary>
    ''' 将一个数组拷贝到缓冲区
    ''' </summary>
    Public Sub WriteBytes(ByVal bytes() As Byte, ByVal iOffset As Integer, ByVal iSize As Integer)
        WriteBytes(0, bytes, 0, bytes.Length)
    End Sub

    ''' <summary>
    ''' 将一个数组拷贝到缓冲区
    ''' </summary>
    Public Sub WriteBytes(ByVal iBeginIndex As Integer,
                          ByVal bytes() As Byte, ByVal iOffset As Integer, ByVal iSize As Integer)
        If (iBeginIndex + iSize) > mSize Then
            Throw New ArgumentOutOfRangeException("iSize"， "需要拷贝的数据块过大")
        End If
        Array.Copy(bytes, iOffset, _Buffer, mOffset + iBeginIndex, iSize)
        ReadableBytes = iSize + iBeginIndex
    End Sub
End Class
